package com.dingpeikeji.quickdeer.modules.rentproduct.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dingpeikeji.quickdeer.core.common.persistence.Page;
import com.dingpeikeji.quickdeer.core.modules.site.entity.TSitedtl;
import com.dingpeikeji.quickdeer.core.modules.sys.entity.User;
import com.dingpeikeji.quickdeer.core.modules.sys.utils.UserUtils;
import com.dingpeikeji.quickdeer.modules.rentproduct.dao.DemandsPriceDao;
import com.dingpeikeji.quickdeer.modules.rentproduct.dao.TDemandsDao;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.DemandsReact;
import com.dingpeikeji.quickdeer.modules.rentproduct.entity.TDemands;
import com.dingpeikeji.quickdeer.modules.sms.entity.Sms;

/**
 * 需求报价
 */
@Service
@Transactional(readOnly = true)
public class DemandsPriceService {

	@Autowired
	private DemandsPriceDao demandsPriceDao;
	@Autowired
	private TDemandsDao demandsDao;
	
	public TDemands get(String demandid) {
		return demandsDao.get(demandid);
	}
	
	/**
	 * 需求报价列表
	 */
	public Page<TDemands> findPage(Page<TDemands> page, TDemands tDemands) {
		// 只能看待反馈的
		//tDemands.setReactstatus(TDemands.REACTSTATUS_WAIT);
		tDemands.setPage(page);
		page.setList(demandsDao.findList(tDemands));
		return page;
	}
	
	/**
	 * 待反馈报价列表
	 */
	public Page<TDemands> findWaitList(Page<TDemands> page, TDemands tDemands) {
		// 只能看待反馈的和非本公司反馈的
		User user = UserUtils.getUser();
		tDemands.setReleaseobject(user.getOffice().getId());
		TSitedtl depart = new TSitedtl();
		depart.setArea(user.getOffice().getArea());
		tDemands.setDepartcity(depart);
		tDemands.setPage(page);
		page.setList(demandsDao.findWaitList(tDemands));
		return page;
	}
	
	/**
	 * 已反馈报价列表
	 */
	public Page<DemandsReact> findBusReactList(Page<DemandsReact> page, DemandsReact tReact) {
		// 只能看待反馈的和非本公司反馈的
		User user = UserUtils.getUser();
		tReact.setReactofficeid(user.getOffice().getId());
		tReact.setPage(page);
		page.setList(demandsPriceDao.findBusReactList(tReact));
		return page;
	}
	
	public boolean feedback(DemandsReact demandsReact) throws Exception{
		// 查看该需求是否已经被更改状态
		TDemands query = new TDemands();
		query.setDemandid(demandsReact.getDemandid());

		User user = UserUtils.getUser();
		demandsReact.setCreateBy(user);
		demandsReact.setReactofficeid(user.getOffice().getId());
		if(demandsPriceDao.insertDemandsReact(demandsReact)==0){
			throw new RuntimeException();
		}
		// 更改需求的状态为已反馈
		TDemands demands = new TDemands();
		demands.setDemandid(demandsReact.getDemandid());
		if(demandsPriceDao.updateDemandsStatusDone(demands)==0){
			throw new RuntimeException();
		}
		return true;
	}
	public boolean sendMessage(DemandsReact demandsReact){
		TDemands tdemands = new TDemands();
		tdemands = this.get(Integer.toString(demandsReact.getDemandid()));
		Sms sms = new Sms();
		sms.setMobile(tdemands.getContactphone());
		sms.setContent("尊敬的" + tdemands.getContactor() +"，您在霞客约车平台上发布的"+tdemands.getDepartcity().getSitename() + "-" + tdemands.getDestcity().getSitename()
				+ tdemands.getRentdays() + "日定制包车需求已有车队报价，报价有效时间为2小时，过时车辆将不一定能保证提供，请及时登录平台定制包车查看我的需求中确认支付！");
		if(demandsPriceDao.sendMsg(sms)== 0){
			return false;
		}else{
		   return true;
		}
		
	}
	public List<DemandsReact> findDemandsReactList(TDemands tDemands){
		return demandsDao.findDemandsReactList(tDemands);
	}
}
